package com.skt.common_hr.common
{
    import flash.display.DisplayObject;
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;
    import flash.geom.Point;
    
    import mx.core.IToolTip;
    import mx.events.ToolTipEvent;

    /**
     * 커스텀 툴팁을 생성해주는 매니저
     * 툴팁 속성을 임의로 준 후 가로채고 나서 동적으로 툴팁객체를 생성해서 
     * 기존 툴팁객체를 바꿔치기 하므로 싱글턴으로 제작해야 한다.
     * 
     * @author David Yun(drumcap)
     */
	public class CustomToolTipManager extends EventDispatcher
	{
        public static const UPSIDE:String = "upside";
        public static const DOWNSIDE:String = "downside";
        public static const LEFTSIDE:String = "leftside";
        public static const RIGHTSIDE:String = "rightside";
        
        /**
         * 생성자 싱글턴 방식으로 사용
         * @param target
         */
        public function CustomToolTipManager(target:IEventDispatcher = null)
        {
            super(target);
        }

        private static var _instance:CustomToolTipManager;

        /**
         * 싱글턴 인스턴스 가져온다
         * @return 
         */
        public static function getInstance():CustomToolTipManager
        {
            if (_instance == null)
            {
                _instance = new CustomToolTipManager();
            }
            return _instance;
        }

        /**
         * 좌표를 계산하여 제대로 위치를 잡아준다.
         * @param event
         */
        public function positionTip(event:ToolTipEvent, position:String = CustomToolTipManager.UPSIDE, xOffset:Number = 0, yOffset:Number = 0):void
        {
            var target:DisplayObject = event.currentTarget as DisplayObject;
            var localPoint:Point;
            switch (position)
            {
                case CustomToolTipManager.UPSIDE:
                    localPoint = new Point(target.x, target.y - event.toolTip.height);
                    break;
                case CustomToolTipManager.DOWNSIDE:
                    localPoint = new Point(target.x, target.y + target.height);
                    break;
                case CustomToolTipManager.LEFTSIDE:
                    localPoint = new Point(target.x - event.toolTip.width, target.y);
                    break;
                case CustomToolTipManager.RIGHTSIDE:
                    localPoint = new Point(target.x + target.width, target.y);
                    break;
            }

            var p:Point = target.parent.localToGlobal(localPoint);
//            var p:Point = (event.toolTip as CustomTooltip).myPosition;
            event.toolTip.x = p.x + xOffset;
            event.toolTip.y = p.y + yOffset;
        }

        /**
         * 커스텀 툴팁을 생성한다.
         * 
         * @param tooltipText
         * @param position
         * @param event
         * @param styleName
         */
        public function createCustomTooltip(tooltipText:String, event:ToolTipEvent, styleName:Object = null):void
        {
            var cusTool:CustomTooltip = new CustomTooltip();
            cusTool.tooltipText = tooltipText;
            cusTool.styleName = styleName;
//            cusTool.myPosition = position;

            event.toolTip = cusTool as IToolTip;
        }

        /**
         * 커스텀 툴팁을 생성한다.
         * 
         * @param tooltipText
         * @param position
         * @param event
         * @param styleName
         */
        public function createCustomTooltip2(tooltipText:String, styleName:Object = null):IToolTip
        {
            var cusTool:CustomTooltip = new CustomTooltip();
            cusTool.tooltipText = tooltipText;
            cusTool.styleName = styleName;
//            cusTool.myPosition = position;
			cusTool.visible = true;
            return cusTool as IToolTip;
        }
    }
}

import flash.geom.Point;

import mx.containers.Box;
import mx.core.IToolTip;
import mx.core.UITextField;

internal class CustomTooltip extends Box implements IToolTip
{
    /**
     * 툴팁 텍스트
     * @default 
     */
    public var tooltipText:String;

    /**
     * 위치
     * @default 
     */
    public var myPosition:Point;

    private var _lb:UITextField;

    /**
     * 내장 클래스 생성자
     */
    public function CustomTooltip()
    {
        super();
        setStyle("verticalAlign", "middle");
        setStyle("horizontalAlign", "center");
    }

    override protected function createChildren():void
    {
        _lb = new UITextField();
        addChild(_lb);
        _lb.text = tooltipText;

        _lb.setStyle("color", getStyle("color"));
    }

    // 아무것도 아님..
    private var _text:String;

    /**
     * IToolTip 구현
     * @return 
     */
    public function get text():String
    {
        return _text;
    }

    /**
     * IToolTip 구현
     * @param value
     */
    public function set text(value:String):void
    {
        if (value != _text)
        {
            _text = value;
        }
    }
}